﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class UClaimDemageControlController : BaseController
    {
        /// <summary>
        /// 添加索赔主记录_服务部填写
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(UClaimDemageControlInfo obj)
        {
            DC.Remove(GetALLClaimsNumberKey);
            return UClaimDemageControlDAL.Add(obj);
        }
        /// <summary>
        /// 删除,将删主从表所有记录
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static bool Delete(string ClaimsNumber)
        {
            DC.Remove(GetALLClaimsNumberKey);
            return UClaimDemageControlDAL.Delete(ClaimsNumber);
        }
        /// <summary>
        /// 根据索赔单号获取主表记录
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static UClaimDemageControlInfo GetByClaimsNumber(string ClaimsNumber)
        {
            UClaimDemageControlInfo obj = CBO.FillObject<UClaimDemageControlInfo>(UClaimDemageControlDAL.GetByClaimsNumber(ClaimsNumber));
            if (obj == null)
                obj = new UClaimDemageControlInfo();
            return obj;

        }
        /// <summary>
        /// 根据索赔单号获取从表所有记录
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static List<UClaimDemageControlDetailInfo> GetDetailByClaimsNumber2(string ClaimsNumber)
        {
            List<UClaimDemageControlDetailInfo> obj = CBO.FillCollection<UClaimDemageControlDetailInfo>(UClaimDemageControlDAL.GetDetailByClaimsNumber2(ClaimsNumber));
            if (obj == null)
                obj = new List<UClaimDemageControlDetailInfo>();
            return obj;

        }
        public static DataTable GetDetailByClaimsNumber(string ClaimsNumber)
        {
           
            return UClaimDemageControlDAL.GetDetailByClaimsNumber(ClaimsNumber);

        }

        /// <summary>
        /// 分页 
        /// </summary>
        /// <param name="SQLWhere"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTableCollection GetPagination(string SQLWhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return UClaimDemageControlDAL.GetPagination(SQLWhere, PageIndex, PageSize, Sort, ref TotalCount);
        }
        public static DataTableCollection GetSupplerDetailPagination(string SQLWhere, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return UClaimDemageControlDAL.GetSupplerDetailPagination(SQLWhere, PageIndex, PageSize, Sort, ref TotalCount);
        }
        /// <summary>
        /// 更新主表记录,根据ClaimsNumber
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Update(UClaimDemageControlInfo obj)
        {
            DC.Remove(GetALLClaimsNumberKey);
            return UClaimDemageControlDAL.Update(obj);
        }
        /// <summary>
        /// 更新结算情况,单条更新
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool UpdateFinalDetail(UClaimDemageControlDetailInfo obj)
        {
            if (obj.ControlDetailID == -1)
                return true;
            DC.Remove(GetALLSuoPeiNumKey);
            return UClaimDemageControlDAL.UpdateFinalDetail(obj);
        }
        /// <summary>
        /// 更新向供应商索赔情况,如果ControlDetailID=0,则表示要新增
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool UpdateSupplerDetail(UClaimDemageControlDetailInfo obj)
        {  
            if (obj.ControlDetailID == -1)
                return true;
            DC.Remove(GetALLSuoPeiNumKey);
            return UClaimDemageControlDAL.UpdateSupplerDetail(obj);
        }
        /// <summary>
        /// 导出明细
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public static DataTableCollection ExportDetailExecl(string sqlwhere)
        {
            return UClaimDemageControlDAL.ExportDetailExecl(sqlwhere);
        }

        /// <summary>
        /// 获取索赔统计报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection GetSupplerTongJiReport(string fromdate, string todate)
        {
            return UClaimDemageControlDAL.GetSupplerTongJiReport(fromdate, todate);
        }
        /// <summary>
        /// 获取品情单号自动完成
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetALLClaimsNumber(string key, int toprow)
        {
            DataTable dt = DC.Get<DataTable>(GetALLClaimsNumberKey);
            if (dt == null)
            {
                dt = UClaimDemageControlDAL.GetALLClaimsNumber();
                DC.Add(GetALLClaimsNumberKey, dt);
            }

            DataRow[] drs;
            if (string.IsNullOrEmpty(key))
                drs = dt.Select("");
            else
                drs = dt.Select("ClaimsNumber like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        /// <summary>
        /// 获取品情单号自动完成
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetALLSuoPeiNum(string key, int toprow)
        {
            DataTable dt = DC.Get<DataTable>(GetALLSuoPeiNumKey);
            if (dt == null)
            {
                dt = UClaimDemageControlDAL.GetALLSuoPeiNum();
                DC.Add(GetALLSuoPeiNumKey, dt);
            }

            DataRow[] drs;
            if (string.IsNullOrEmpty(key))
                drs = dt.Select("");
            else
                drs = dt.Select("SuoPeiNum like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        /// <summary>
        /// 供应商市厌件统计表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTableCollection SupplierShiYanTongJi(string sqlwhere)
        {
            return UClaimDemageControlDAL.SupplierShiYanTongJi(sqlwhere);
        }

        /// <summary>
        /// 获取二次派工比率报表
        /// </summary>
        /// <param name="fromdate"></param>
        /// <param name="todate"></param>
        /// <returns></returns>
        public static DataTable GetPaiGongCiShuBiLi(string fromdate, string todate)
        {
            return UClaimDemageControlDAL.GetPaiGongCiShuBiLi(fromdate, todate);
        }
    }
}
